import { createSlice } from '@reduxjs/toolkit'

const initialState = {
    datas: [
    ]
}

export const cartSlice = createSlice({
    name: 'cart',
    initialState,
    reducers: {
        clear: (state) => {
            state.datas = []
        },
        uncheck: (state, action) => {
            const { name } = action.payload;
            const good = state.datas.find(item => item.name == name);
            if (good) {
                good.uncheck = !good.uncheck
            }
            else {
                state.datas.push({
                    name: name,
                    num: 0,
                    uncheck: true
                })
            }
        },
        changeNum: (state, action) => {
            const { name, num } = action.payload
            const good = state.datas.find(item => item.name == name)
            if (good) {
                good.num = num
            }
            else {
                state.datas.push({
                    name,
                    num
                })
            }
        },
    },
})